//
//  GFCommentsTableViewCell.m
//  GoodFood
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFCommentsTableViewCell.h"

@interface GFCommentsTableViewCell ()

@property (nonatomic, strong) UIImageView *userImage;
@property (nonatomic, strong) UILabel *userLabel;
@property (nonatomic, strong) UILabel *commentLabel;
@property (nonatomic, strong) UILabel *userTag;

@end

@implementation GFCommentsTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.userImage = [[UIImageView alloc] init];
        self.userImage.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:self.userImage];
        
        self.userLabel = [[UILabel alloc] init];
        self.userLabel.font = [UIFont systemFontOfSize:17];
        self.userLabel.textColor = [UIColor colorWithRed:1.000 green:0.448 blue:0.636 alpha:0.780];
        [self.contentView addSubview:self.userLabel];
        
        self.commentLabel = [[UILabel alloc] init];
        self.commentLabel.numberOfLines = 0;
        self.commentLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.commentLabel];
        
        self.userTag = [[UILabel alloc] init];
        self.userTag.layer.cornerRadius = 5;
        self.userTag.layer.masksToBounds = YES;
        self.userTag.font = [UIFont systemFontOfSize:13];
        self.userTag.textAlignment = NSTextAlignmentCenter;
        self.userTag.textColor = [UIColor whiteColor];
        [self.contentView addSubview:self.userTag];
        
    }
    return self;
}

-(void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.userImage.frame = CGRectMake(10, 10, ScreenWidth / 10, ScreenWidth / 10);
    
    CGRect width = [self.tempDic[@"user_name"] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    self.userLabel.frame = CGRectMake(10 + ScreenWidth / 10 + 10, 10, width.size.width, ScreenWidth / 20);
    
    CGRect widthTag = [self.tempDic[@"level_title"] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    self.userTag.frame = CGRectMake(20 + ScreenWidth / 10 + 10 + width.size.width, 10, widthTag.size.width, ScreenWidth / 20);
    
    CGRect height = [self.tempDic[@"comment"] boundingRectWithSize:CGSizeMake(ScreenWidth - (ScreenWidth / 10 + 30), 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    self.commentLabel.frame = CGRectMake(ScreenWidth / 10 + 20, 10 + ScreenWidth / 20, ScreenWidth - (ScreenWidth / 10 + 20), height.size.height);
    
    
}

-(void)setTempDic:(NSDictionary *)tempDic {
    
    if (_tempDic != tempDic) {
        _tempDic = tempDic;
    }
    
    if (tempDic) {
        [self.userImage setImageWithURL:[NSURL URLWithString:tempDic[@"avatar"]] placeholderImage:nil];
        
        self.userLabel.text = tempDic[@"user_name"];
        if ([tempDic[@"level_title"] isEqualToString:@"路人"]) {
            self.userTag.backgroundColor = [UIColor brownColor];
        } else if ([tempDic[@"level_title"] isEqualToString:@"食客"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.089 green:0.409 blue:1.000 alpha:1.000];
        } else if ([tempDic[@"level_title"] isEqualToString:@"美食爱好者"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.926 green:0.248 blue:0.845 alpha:1.000];
        } else if ([tempDic[@"level_title"] isEqualToString:@"美食评论员"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.227 green:0.926 blue:0.249 alpha:1.000];
        } else if ([tempDic[@"level_title"] isEqualToString:@"美食专家"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.050 green:0.267 blue:0.078 alpha:1.000];
        }
        self.userTag.text = tempDic[@"level_title"];
        self.commentLabel.text = tempDic[@"comment"];
        
    }
    
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
